import { createRouter, createWebHistory } from "vue-router";
import type { RouteRecordRaw } from 'vue-router'
const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "login",
    component: () => import("@/views/login.vue"),
    meta: {
      title: "登录",
      keepAlive: true,
    },
  },
  {
    path: "/home",
    name: "Home",
    component: () => import("@/views/home.vue"),
    meta: {
      title: "首页",
      keepAlive: true,
    },
  },
  {
    path: "/myRecord",
    name: "myRecord",
    component: () => import("@/views/myRecord.vue"),
    meta: {
      title: "我的记录",
      keepAlive: true,
    },
  },
  {
    path: "/bookIn",
    name: "bookIn",
    component: () => import("@/views/bookIn.vue"),
    meta: {
      title: "漂入",
      keepAlive: true,
    },
  },
  {
    path: "/bookOut",
    name: "bookOut",
    component: () => import("@/views/bookOut.vue"),
    meta: {
      title: "漂出",
      keepAlive: true,
    },
  },
  {
    path: "/404",
    name: "NotFound",
    component: () => import("@/views/NotFound.vue"),
    meta: {
      title: "404",
      keepAlive: false,
    },
  },
  {
    path: "/scan",
    name: "scan",
    component: () => import("@/views/scan.vue"),
    meta: {
      title: "扫码",
      keepAlive: false,
    },
  },
  {
    path: "/updatePwd",
    name: "updatePwd",
    component: () => import("@/views/updatePwd.vue"),
    meta: {
      title: "更新密码",
      keepAlive: false,
    },
  },
  {
    path: "/aboutUs",
    name: "aboutUs",
    component: () => import("@/views/aboutUs.vue"),
    meta: {
      title: "关于我们",
      keepAlive: false,
    },
  },
  {
    path: "/bookList",
    name: "bookList",
    component: () => import("@/views/bookList.vue"),
    meta: {
      title: "可漂出书籍",
      keepAlive: false,
    },
  },
  {
    path: "/:pathMatch(.*)*",
    redirect: "/404",
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  console.log(from)
  // 设置页面标题
  document.title = `${to.meta.title || ""} - ${import.meta.env.VITE_APP_TITLE}`;
  next();
});

export default router;
